package cn.com.callback;
import groovy.json.*;
import groovy.swing.impl.DefaultAction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.elite.EliteBean;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wisdge.dataservice.Result;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import cn.com.elite.Util;

/**
 * 
 * @Title: apiSyncCustData
 * @Description:大数金融第三方系统名单同步接口
 * @author: LiJuntao
 * @date 2019-11-27 下午4:20:53
 *
 */

public class apiSyncCustData implements ServiceBackInvoke{
	private static final Log log = LogFactory.getLog(apiSyncCustData.class);
	public String doCallBack(EliteBean eliteBean,HttpServletRequest request,HttpServletResponse response) {

		response.setCharacterEncoding("UTF-8");

		String getMethod = request.getMethod();
		log.debug("getMethod:" + getMethod);

		String inData = "";
		String outData = "";
		String dataSourceID = "dataSource";

		BufferedReader iny = null;
		if (getMethod.equals("GET")) {
			inData = request.getParameter("data");
			log.debug("原始Get请求参数:" + inData);
		} else {
			InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
			iny = new BufferedReader(reader);
			String line;
			while ((line = iny.readLine()) != null) {
				inData += line;
			}
			log.debug("原始Post请求参数:" + inData);
		}

		try {
			// 调用处理信息
			JSONObject inObject = new JSONObject(inData);
			log.debug("处理前的Json：" + inObject.toString());

			String sys_source = GetJV(inObject, "sys_source");
			String sys_flag = GetJV(inObject, "sys_flag");
			String arr_data = GetJV(inObject, "data");

			log.debug("解析登录信息-sys_source：" + sys_source + " sys_flag：" + sys_flag + " arr_data：" + arr_data);

			outData = wxDaShuTong(sys_source, sys_flag, arr_data, dataSourceID);

		} catch (Exception e) {
			log.error("接口处理异常" + e.getMessage());
			// 失败返回
			outData = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):2\",\"data\":[]}";

		}

		log.debug("名单接入处理完成返回结果:" + outData);

		return outData;
	}

	// 大数通名单接入处理
	public static String wxDaShuTong(String sys_source, String sys_flag, String inJson, String dataSourceID) {
		long startTime = System.currentTimeMillis();
		JSONArray arrJsonArray;
		Map<String, String> dealMap = new HashMap<String, String>();
		JSONArray resArray = new JSONArray();
		String reJson = null;
		try {
			arrJsonArray = new JSONArray(inJson);

			int arrInt = arrJsonArray.length();
			log.debug("接入List数量:" + arrInt);

			for (int i = 0; i < arrInt; i++) {
				JSONObject arrCustJson = arrJsonArray.getJSONObject(i);

				String id = GetJV(arrCustJson, "id");
				/*String applicationdate = GetJV(arrCustJson, "applicationdate");
				String applicationid = GetJV(arrCustJson, "applicationid");
				String custname = GetJV(arrCustJson, "custname");
				String certificatenum = GetJV(arrCustJson, "certificatenum");
				String mobiletelephonenum = GetJV(arrCustJson, "mobiletelephonenum");
				String stepstatus = GetJV(arrCustJson, "stepstatus");
				String rating = GetJV(arrCustJson, "rating");
				String loanableamount = GetJV(arrCustJson, "loanableamount");*/

				dealMap.put(id, "1");

			}

			// 将每条处理结果转换为JSONArray
			int j = 0;
			for (Map.Entry<String, String> m : dealMap.entrySet()) {
				JSONObject resObject = new JSONObject();
				resObject.put("id", m.getKey());
				resObject.put("code", m.getValue());
				resArray.put(j, resObject);
				j++;
			}

			log.debug(resArray.toString());

			long endTime = System.currentTimeMillis();
			// 包装拼接返回JSONObject
			reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"处理完成、调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + resArray.toString() + "}";

		} catch (JSONException e) {
			long endTime = System.currentTimeMillis();
			reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(" + e.getMessage() + "),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
		}

		return reJson;
	}


	// 解析Json值
	public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null;
		try {
			Rev_Value = inJson.getString(getKey);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey));
			} catch (Exception e1) {
				try {
					Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
				} catch (Exception e2) {
					try {
						Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
					} catch (Exception e3) {
						Rev_Value = "";
					}
				}
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				Rev_Value = "";
			} else {
			}
		}
		return Rev_Value;
	}

}
